

function latex_table = create_latex_table_JL(data, k_values, JLErrorTypeVec, caption_text)
    if nargin < 4
        caption_text = '';
    end
    
    x = data;
    x_tex = cellfun(@(v) sprintf('%g', v), num2cell(x), 'UniformOutput', false);
    for i = 1:size(x, 1)
        for j = 1:size(x, 2)
            exp = floor(log10(x(i,j)));
            mant = x(i,j) / 10^exp;
            x_tex{i,j} = sprintf('$%.1f \\times 10^{%d}$', mant, exp);
            if exp == -1 || exp == 0 || exp == -2 
                x_tex{i,j} = sprintf('$%.2f$', x(i,j));
            end
            if isnan(x(i,j))
                x_tex{i,j} = 'NaN';
            end
            if j == 1 || j == 3
                if JLErrorTypeVec(i, 1) == 1
                    x_tex{i,j} = 'Polyfit error';
                end
            elseif j == 2 || j == 4
                if JLErrorTypeVec(i, 2) == 1
                    x_tex{i,j} = 'Polyfit error';
                elseif JLErrorTypeVec(i, 3) == 1
                    x_tex{i,j} = 'Not converged';
                end
            end
        end
    end
    data = x_tex;
    
    table_content = ["\\centering{}", ...
                     "\\begin{tabular}{ccccccccc}", ...
                     "\\hline", ...
                     " &  & \\multicolumn{3}{c}{{\\footnotesize{}Infinity Norm}} &  & \\multicolumn{3}{c}{{\\footnotesize{}Root Mean Square Error}}\\tabularnewline", ...
                     "\\cline{3-5} \\cline{7-9}", ...
                     "{\\footnotesize{}K} &  & {\\footnotesize{}First Order} &  & {\\footnotesize{}Second Order} &  & {\\footnotesize{}First Order} &  & {\\footnotesize{}Second Order}\\tabularnewline", ...
                     "\\hline", ...
                     "\\hline"];

    for i = 1:length(k_values)
        row = sprintf('{\\footnotesize %d} &  & %s &  & %s &  & %s &  & %s\\tabularnewline', k_values(i), data{i, 1}, data{i, 2}, data{i, 3}, data{i, 4});
        table_content(end + 1) = row;
    end

    table_content(end + 1) = "\\hline";
    table_content(end + 1) = "\\end{tabular}";
    
%     if ~isempty(caption_text)
%         table_content(end + 1) = strcat("\\caption{", caption_text, "}");
%     end
    
    %table_content(end + 1) = "\\end{table}";

    latex_table = strjoin(table_content, '\n');
    latex_table = strrep(latex_table, '\\', '\');
end
